<template>
  <div>
    <img @click="onClickImg(i)" v-for="(item,i) in newImgs" :key="i" :src="item" class="img-item" />

    <div v-if="show" class="view-img-popup">
      <img  :src="newImgs[checkIndex]" class="check-img" />
      <div @click="show=!show" class="close">关闭</div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    imgs: {
      type: [Array ,String],
      default() {return {}}
    },
    key: {
      type: String,
      default: 'url'
    }
  },
  data() {
    return {
      checkIndex: 0,
      show:false,
    }
  },
  computed:{
    newImgs() {
      if(Array.isArray(this.imgs) && this.imgs.length) {
        if(typeof this.imgs[0] == 'object') {
          // 判断是否有值
          if(this.imgs[0][this.key]) {
            return this.imgs.map(item => item[key])
          } else {
            return []
          }
          
        } else if(typeof this.imgs[0] == 'string') {
          return this.imgs
        }
      } else if (typeof this.imgs == 'string') {
        return [this.imgs]
      } 
      return []
    }
  },
  methods: {
    onClickImg(index) {
      this.checkIndex = index
      this.show = !this.show
    }
  }
}
</script>

<style lang="scss" scoped>
.img-item {
  margin-right: 10px;
  margin-bottom: 10px;
  width: 150px;
  height: 150px;
  border-radius: 4px;
}
.view-img-popup {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
  z-index: 99999;
  .check-img {
    width: 40vw;
    height: 40vw;
    border-radius: 10px;
  }
  .close {
    color: #f0250e;
    position: absolute;
    right: 10px;
    top: 0;
    padding: 10px;
    cursor: pointer;
  }
}
</style>
